const jwt = require('jsonwebtoken')
const white = [
  '/api/login',
  '/api/register',
  '/api/findRoom',
  '/api/findCourse'
]
module.exports = (options, app) => {
  return async (ctx, next) => {
    if (white.includes(ctx.path)) {
      await next()
    } else {
      try {
        const { authorization } = ctx.request.header;
        const data = jwt.verify(authorization, app.config.keys)
        ctx.state.user = data; // 权限验证的时候时候
        await next() // 
      } catch (err) {
        ctx.body = { code: -1, msg: err.message }
      }
    }
  }
}